<?php
session_start();
if(!@$_SESSION['username']) {
  echo "<script>window.location.href='login.php';</script>";
}
?><!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>车票退订</title>
</head>
<body>
	<?php
		include_once('../../libraries/esaydb.class.php');
		if($_GET) {
			$rid = $_GET['rid'];
			$db = new EasyDB();
			$where = array(
				'rid' => $rid
            );
            $del_s = "SELECT ispay FROM ob_order WHERE rid = ?";
            $del_result = $db->queryOne($del_s, $rid);
            if($del_result['ispay'] == 'no') { 
                $result = $db->delete('ob_order',$where);
                if($result) {
                    echo "<script>alert('退订成功');window.location.href='../../order-list.php'</script>";
                }else{     
                    echo "<script>alert('退订失败');window.location.href='../../order-list.php'</script>";
                }
            }
			$result = $db->delete('ob_order',$where);
			if($result) { 
                //票数加一
                $sid = $_GET['sid'];
                $type = $_GET['site_type'];
                if($type == 'have') {
                    $sql_s = "SELECT number FROM ob_station WHERE id = ?";
                    $result_s = $db->queryOne($sql_s, array($_GET['sid']));
                    if($result_s['number'] <= 0) {
                      echo "<script>alert('车票不足');history.go(-1);</script>";exit(1);
                    }
                    $result_s['number']++;
                    $db->update("ob_station",array('number'=>$result_s['number']),array('id'=>$_GET['sid']));
                }else if($type == 'none') {
                    $sql_s = "SELECT num_none FROM ob_station WHERE id = ?";
                    $result_s = $db->queryOne($sql_s, array($_GET['sid']));
                    if($result_s['num_none'] <= 0) {
                      echo "<script>alert('车票不足');history.go(-1);</script>";exit(1);
                    }
                    $result_s = $db->queryOne($sql_s, array($_GET['sid']));
                    $result_s['num_none']++;
                    $db->update("ob_station",array('num_none'=>$result_s['num_none']),array('id'=>$_GET['sid']));
                }else{
                    echo "<script>alert('操作失败');history.go(-1);</script>";
                }

                echo "<script>alert('退订成功');window.location.href='../../order-list.php'</script>";
			}else{
				echo "<script>alert('退订失败');history.go(-1);</script>";
			}
		}else{
			echo "<script>alert('参数错误');history.go(-1);window.close();';</script>";
		}
	?>
</body>
</html>
